<template>
  <el-card class="student-info-card">
    <h2>学生信息管理</h2>
    <el-descriptions v-if="info && info.id" :column="1" border style="min-width: 700px; width: 100%;">
      <el-descriptions-item label="姓名">{{ info.name }}</el-descriptions-item>
      <el-descriptions-item label="性别">{{ info.gender === 1 ? '女' : '男' }}</el-descriptions-item>
      <el-descriptions-item label="出生日期">{{ info.birthDate }}</el-descriptions-item>
      <el-descriptions-item label="手机号">{{ info.phone }}</el-descriptions-item>
      <el-descriptions-item label="邮箱">{{ info.email }}</el-descriptions-item>
      <el-descriptions-item label="院校">{{ info.school }}</el-descriptions-item>
      <el-descriptions-item label="专业">{{ info.major }}</el-descriptions-item>
      <el-descriptions-item label="学历">{{ info.degree }}</el-descriptions-item>
      <el-descriptions-item label="毕业时间">{{ info.graduateDate }}</el-descriptions-item>
      <el-descriptions-item label="项目经历">{{ info.projectExperience }}</el-descriptions-item>
      <el-descriptions-item label="获奖情况">{{ info.award }}</el-descriptions-item>
      <el-descriptions-item label="证书">{{ info.certificate }}</el-descriptions-item>
    </el-descriptions>
    <el-button type="primary" class="mt-2" @click="showEdit = true">编辑信息</el-button>
    <el-dialog v-model="showEdit" title="编辑学生信息" width="600px">
      <el-form :model="editForm" label-width="110px" ref="formRef">
        <el-form-item label="学生ID">
          <el-input v-model="editForm.id" disabled placeholder="学生ID不可修改" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="editForm.name" disabled placeholder="姓名不可修改" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="editForm.gender">
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="editForm.birthDate" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editForm.phone" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editForm.email" />
        </el-form-item>
        <el-form-item label="院校">
          <el-input v-model="editForm.school" />
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="editForm.major" />
        </el-form-item>
        <el-form-item label="学历">
          <el-input v-model="editForm.degree" />
        </el-form-item>
        <el-form-item label="毕业时间">
          <el-date-picker v-model="editForm.graduateDate" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="项目经历">
          <el-input v-model="editForm.projectExperience" type="textarea" />
        </el-form-item>
        <el-form-item label="获奖情况">
          <el-input v-model="editForm.award" type="textarea" />
        </el-form-item>
        <el-form-item label="证书">
          <el-input v-model="editForm.certificate" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showEdit = false">取消</el-button>
        <el-button type="primary" @click="onSave" :loading="loading">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getStudentInfo, updateStudentInfo } from '@/api/student'
import {getEnterpriseInfo} from "@/api/enterprise.js";

const info = ref({})
const showEdit = ref(false)
const editForm = ref({})
const loading = ref(false)
const formRef = ref()

const fetchInfo = async () => {
  try {
    const res = await getStudentInfo()
    info.value = res.studentInfo || res
    editForm.value = { ...res.studentInfo } || {}
  } catch (e) {}
}
const onSave = async () => {
  loading.value = true
  try {
    // 只提交非空字段，id必传
    const data = {}
    Object.keys(editForm.value).forEach(key => {
      if (
          editForm.value[key] !== '' &&
          editForm.value[key] !== null &&
          editForm.value[key] !== undefined
      ) {
        data[key] = editForm.value[key]
      }
    })
    await updateStudentInfo(data)
    ElMessage.success('学生信息更新成功')
    showEdit.value = false
    fetchInfo()
  } catch (e) {}
  loading.value = false
}
onMounted(() => {
  fetchInfo()
})
</script>

<style scoped>
.student-info-card {
  max-width: 900px;
  margin: 40px auto;
  padding: 30px 20px;
}
</style>